package cn.skyquant.quant4j.jforex.sdk.strategy;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MethodProcessManager {
    private Map<String,List<MethodProcessTotal>> map = new HashMap();

    private long limit;
    private PrintStream out;
    public MethodProcessManager(long limit,PrintStream out) {
        this.limit = limit;
        this.out = out;
    }

    public void add(String name,long begin,long end){
        List<MethodProcessTotal> list = map.get(name);
        if(list==null){
            list = new ArrayList<>();
            map.put(name,list);
        }
        MethodProcessTotal mpt = new MethodProcessTotal(begin,end);
        list.add(mpt);
        if(mpt.time > limit){
            out.println(name+".time="+mpt.time);
        }

    }

    public void print(){
        map.forEach((k,v)->{
            int s = v.size();
            int total_time = 0;
            long max_time =0 ;
            for(MethodProcessTotal m:v){
                total_time+=m.time;
                if(m.time>max_time){
                    max_time = m.time;
                }
            }
            out.println(k+",s="+s+",total="+total_time+",avg="+(total_time/s)+",max_time="+max_time);
        });
    }
}
